//
//  ;
//  BirdLoveSheep
//
//  Created by wwj on 16/3/22.
//  Copyright © 2016年 wwj. All rights reserved.
//

#import "FWContentImgView.h"
#import "FWTopicModel.h"
#import <UIImageView+WebCache.h>
#import "FWProgressRingView.h"
#import "FWPhotoGroupView.h"

@interface FWContentImgView ()
@property (weak, nonatomic) IBOutlet UIImageView *gifView;
@property (weak, nonatomic) IBOutlet UIButton *seeBigButton;
@property (weak, nonatomic) IBOutlet FWProgressRingView *progressRingView;

@end

@implementation FWContentImgView

+ (instancetype)contentImgView{
    return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil] lastObject];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    self.imageView.userInteractionEnabled = YES;
    [self.imageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickImg)]];
}

- (void)onClickImg{
    FWPhotoGroupView *photosView = [FWPhotoGroupView photoGroupView];
    [photosView show:self.imageView];
    photosView.model = self.model;
}

- (void)setModel:(FWTopicModel *)model{
    [super setModel:model];
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:model.image2] placeholderImage:nil options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        self.progressRingView.hidden = NO;
        model.progress = 1.0 * receivedSize / expectedSize;
        [self.progressRingView setProgress:model.progress animated:YES];
        
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        self.progressRingView.hidden = YES;
        if (model.isBigImg) {
            UIGraphicsBeginImageContextWithOptions(self.imageView.size, YES, 0.0);
            CGFloat with = kScreenW - 2 * LRMARGIN;
            CGFloat height = with / image.size.width * image.size.height;
            [image drawInRect:CGRectMake(0, 0, with, height)];
            self.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
        }
    }];
    
    self.gifView.hidden = ![model.image2.pathExtension.lowercaseString isEqualToString:@"gif"];
    self.seeBigButton.hidden = !model.isBigImg;
}



@end
